<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\Random;
/**
 * 商家管理
 *
 * @icon fa fa-circle-o
 */
class Business extends Backend
{
    
    /**
     * Business模型对象
     * @var \app\admin\model\Business
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Business');

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index() {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $sort = 'a.'.$sort;
            $total = $this->model
                    ->alias('a')
                    ->field('a.*,b.cash_amount,b.frozen_amount,c.status')
                    ->join('business_wallet b','b.business_id = a.business_id')
                    ->join('admin c','c.id = a.admin_id')
                    ->where($where)
                    ->order($sort, $order)
                    ->count();
            $list = $this->model
                    ->alias('a')
                    ->field('a.*,b.cash_amount,b.frozen_amount,c.status')
                    ->join('business_wallet b','b.business_id = a.business_id','left')
                    ->join('admin c','c.id = a.admin_id','left')
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            if($list){
                foreach($list as &$val){
                    $val['status_text'] = $val['status'] == 'normal' ? '正常' : '禁用';
                    
                    $cert_status_text = '';
                    switch ($val['cert_status']) {
                        case '0':
                            $cert_status_text = '待认证';
                            break;
                        case '1':
                            $cert_status_text = '已通过';
                            break;
                        case '2':
                            $cert_status_text = '已拒绝';
                            break;
                    }
                    $val['cert_status_text'] = $cert_status_text;
                }
                unset($val);
            }

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    
    public function updatestatus(){
        $admin_id = input('ids',0,'intval');
        $adminInfo = \think\Db::name('admin')->where(['id' => $admin_id])->find();
        if(!$adminInfo){
            $this->error('无该用户');
        }
        if($adminInfo['status'] == 'normal'){
            $status = '';
        }else{
            $status = 'normal';
        }
        
        $data['status'] = $status;
        $data['updatetime'] = time();
        \think\Db::name('admin')->where(['id' => $admin_id])->update($data);
        $this->success();
    }

    public function checkok(){
        $car_id = input('car_id',0,'intval');
        $data['car_cert'] = 1;
        $data['checktime'] = time();
        \think\Db::name('user_car')->where(['car_id' => $car_id])->update($data);
        $this->success();
    }
    public function checkno(){
        $car_id = input('car_id',0,'intval');
        $refuse_text = input('refuse_text');
        $data['refuse_desc'] = $refuse_text;
        $data['check_time'] = time();
        $data['task_status'] = 10;
        \think\Db::name('user_car')->where(['car_id' => $car_id])->update($data);
        $this->success();
    }
    
    public function detail(){
        $business_id = input('ids',0,'intval');
        $list = \think\Db::name('business_wallet_log')
                ->alias('a')
                ->field('a.*,c.task_no')
                ->join('business_wallet b','a.wallet_id = b.wallet_id')
                ->join('task c','c.task_id = a.task_id','left')
                ->where(['b.business_id'=>$business_id])
                ->select();
        
        if($list){
            foreach($list as &$val){
                switch ($val['change_matters']) {
                    case '1':
                        $change_matters_text = '任务支出';
                        break;
                    case '2':
                        $change_matters_text = '任务酬金退回';
                        break;
                    case '3':
                        $change_matters_text = '平台调整';
                        break;
                    case '4':
                        $change_matters_text = '提现';
                        break;
                    case '10':
                        $change_matters_text = '充值';
                        break;
                }
                $val['change_matters_text'] = $change_matters_text;
            }
            unset($val);
        }
        
        $data = $this->model
                   ->alias('a')
                   ->field('a.*,b.cash_amount')
                   ->join('business_wallet b','b.business_id = a.business_id','left')
                   ->where(['a.business_id'=>$business_id])
                   ->find();
        $this->assign('data',$data);
        $this->assign('list',$list);
        return $this->view->fetch();
    }
    
    public function setwallet(){
        $business_id = input('business_id',0);
        $change_type = input('adjust_way',0,'intval');
        $change_amount = input('adjust_price',0);
        $change_matters = 3;    //变动事项 1-任务支出 2-任务酬金退回 3-平台调整 4-提现  10-充值
        $arrOther['remark'] = input('adjust_desc');
        
        if(empty($business_id)){
            $this->error('没有选中商家ID');
        }
        $this->change_business_wallet($business_id, $change_type, $change_amount, $change_matters, $arrOther);
        $this->success('操作成功');
    }
    
    public function resetpassword(){
        $password = '123456';
        $admin_id = input('ids',0,'intval');
        $adminData['salt'] = Random::alnum();
        $adminData['password'] = md5(md5($password) . $adminData['salt']);
        $adminData['updatetime'] = time();
        \think\Db::name('admin')->where(['id' => $admin_id])->update($adminData);
        $this->success();
    }

}
